#include "deb.h"
#include "ui_deb.h"
#include "mainwindow.h"
#include <QTextCodec>
#include <QMessageBox>
#include <string>

//发送缓冲区
QString strbuf;

Deb::Deb(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Deb)
{
    ui->setupUi(this);
    setFixedSize(460,450);
    ui->rec_edi->setReadOnly(true);//设置只读
    this->setWindowTitle("Debug");
    this->setWindowIcon(QIcon(":/img/app.ico"));

}

Deb::~Deb()
{
    delete ui;
}

void Deb::DisplayData(QString qstring){
    ui->rec_edi->appendPlainText(qstring);
}

void Deb::keyPressEvent(QKeyEvent *event)
{
    switch(event->key()){
            case Qt::Key_Escape: //监听退出键
                qDebug()<<("Key_Escape Press");
                on_exit_bt_clicked();
                break;
            case Qt::Key_Return: //监听回车键
                qDebug()<<("Key_Enter Press");
                on_send_bt_clicked();
                break;
            default:
                break;
        }
}

//发送数据
void Deb::on_send_bt_clicked()
{

    MainWindow mainwindow ;
    strbuf = ui->send_edi->text() ;
    // 发送数据的格式
    QString str = "->"; //添加每个数据的帧头
    QString str2 = str.append(strbuf);
    ui->rec_edi->appendPlainText(str2);
    mainwindow.test(strbuf); //
}
//清除发送框
void Deb::on_btn_clear_send_clicked()
{
    ui->send_edi->clear();
}
//清除接受框
void Deb::on_btn_clear_revi_clicked()
{
    ui->rec_edi->clear();
}
//关闭调试界面
void Deb::on_exit_bt_clicked()
{
    this->close();
}
